using System;
using System.Collections.Generic;
using System.Text;

using asmxItinerary = ESBSimpleSamples.Client.ESBProcessItineraryOneWay;
using asmxItinerary2Way = ESBSimpleSamples.Client.ESBProcessItineraryTwoWay;
using System.Xml;

namespace ESBSimpleSamples.Client.ServiceRequests
{
    public class GetUnicodeCode
    {
        public void SubmitRequest(string character)
        {
            //we have three services - map input, route map output, process routed messages:
            asmxItinerary.ItineraryServicesService mapService = new asmxItinerary.ItineraryServicesService();
            mapService.name = "Microsoft.Practices.ESB.Services.Transform";
            mapService.type = "Messaging";
            mapService.state = "Pending";
            mapService.position = 0;
            mapService.isRequestResponse = false;
            mapService.isRequestResponseSpecified = true;

            asmxItinerary.ItineraryServicesService routeService = new asmxItinerary.ItineraryServicesService();
            routeService.name = "Microsoft.Practices.ESB.Services.Routing";
            routeService.type = "Messaging";
            routeService.state = "Pending";
            routeService.position = 1;
            routeService.isRequestResponse = false;
            routeService.isRequestResponseSpecified = true;

            asmxItinerary.ItineraryServicesService sendService = new asmxItinerary.ItineraryServicesService();
            sendService.name = "DynamicTest";
            sendService.type = "Messaging";
            sendService.state = "Pending";
            sendService.position = 2;
            sendService.isRequestResponse = false;
            sendService.isRequestResponseSpecified = true;

            //build the itinerary - add services:
            asmxItinerary.Itinerary itinerary = new asmxItinerary.Itinerary();
            itinerary.Services = new asmxItinerary.ItineraryServices[3];
            itinerary.Services[0] = new asmxItinerary.ItineraryServices();
            itinerary.Services[0].Service = mapService;
            itinerary.Services[1] = new asmxItinerary.ItineraryServices();
            itinerary.Services[1].Service = routeService;
            itinerary.Services[2] = new asmxItinerary.ItineraryServices();
            itinerary.Services[2].Service = sendService;

            //specify service instance (current step = first instance):
            itinerary.ServiceInstance = new asmxItinerary.ItineraryServiceInstance();
            itinerary.ServiceInstance.name = mapService.name;
            itinerary.ServiceInstance.type = mapService.type;
            itinerary.ServiceInstance.state = mapService.state;
            itinerary.ServiceInstance.position = mapService.position;
            itinerary.ServiceInstance.isRequestResponse = false;
            itinerary.ServiceInstance.isRequestResponseSpecified = true;

            //three sets of resolvers in the group:
            itinerary.ResolverGroups = new asmxItinerary.ArrayOfItineraryResolversResolvers[3];
            itinerary.ResolverGroups[0] = new asmxItinerary.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[0].serviceId = string.Format("{0}{1}", mapService.name, mapService.position);
            itinerary.ResolverGroups[0].Value = @"<![CDATA[STATIC:\\TransformType=ESBSimpleSamples.BizTalk.Maps.Parameters_GetUnicodeCodeRequest,ESBSimpleSamples.BizTalk, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a9e52fd7be56a687;]]>";

            itinerary.ResolverGroups[1] = new asmxItinerary.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[1].serviceId = string.Format("{0}{1}", routeService.name, routeService.position);
            itinerary.ResolverGroups[1].Value = @"<![CDATA[STATIC:\\TransportLocation=FILE://C:\ESBSimpleSamples_Unicode_%MessageID%.xml;]]>";

            itinerary.ResolverGroups[2] = new asmxItinerary.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[2].serviceId = string.Format("{0}{1}", sendService.name, sendService.position);


            asmxItinerary.Process process = new asmxItinerary.Process();
            process.Credentials = System.Net.CredentialCache.DefaultCredentials;

            process.ItineraryValue = itinerary;
            XmlNode node = new XmlDocument();
            node.InnerXml = string.Format(@"<ns0:Parameters xmlns:ns0=""http://ESBSimpleSamples.BizTalk.Schemas.Parameters""><ns0:Parameter Name=""Character"" Value=""{0}""/></ns0:Parameters>", character);
            process.SubmitRequest(node);
        }

        public byte[] SubmitRequestResponse(string character)
        {
            //we have three services - map input, route map output, process routed messages:
            asmxItinerary2Way.ItineraryServicesService mapService = new asmxItinerary2Way.ItineraryServicesService();
            mapService.name = "Microsoft.Practices.ESB.Services.Transform";
            mapService.type = "Messaging";
            mapService.state = "Pending";
            mapService.position = 0;
            mapService.isRequestResponse = false;
            mapService.isRequestResponseSpecified = true;

            asmxItinerary2Way.ItineraryServicesService routeService = new asmxItinerary2Way.ItineraryServicesService();
            routeService.name = "Microsoft.Practices.ESB.Services.Routing";
            routeService.type = "Messaging";
            routeService.state = "Pending";
            routeService.position = 1;
            routeService.isRequestResponse = false;
            routeService.isRequestResponseSpecified = true;

            asmxItinerary2Way.ItineraryServicesService sendService = new asmxItinerary2Way.ItineraryServicesService();
            sendService.name = "DynamicResolutionSolicitResp";
            sendService.type = "Messaging";
            sendService.state = "Pending";
            sendService.position = 2;
            sendService.isRequestResponse = false;
            sendService.isRequestResponseSpecified = true;

            //build the itinerary - add services:
            asmxItinerary2Way.Itinerary itinerary = new asmxItinerary2Way.Itinerary();
            itinerary.Services = new asmxItinerary2Way.ItineraryServices[3];
            itinerary.Services[0] = new asmxItinerary2Way.ItineraryServices();
            itinerary.Services[0].Service = mapService;
            itinerary.Services[1] = new asmxItinerary2Way.ItineraryServices();
            itinerary.Services[1].Service = routeService;
            itinerary.Services[2] = new asmxItinerary2Way.ItineraryServices();
            itinerary.Services[2].Service = sendService;

            //specify service instance (current step = first instance):
            itinerary.ServiceInstance = new asmxItinerary2Way.ItineraryServiceInstance();
            itinerary.ServiceInstance.name = mapService.name;
            itinerary.ServiceInstance.type = mapService.type;
            itinerary.ServiceInstance.state = mapService.state;
            itinerary.ServiceInstance.position = mapService.position;
            itinerary.ServiceInstance.isRequestResponse = false;
            itinerary.ServiceInstance.isRequestResponseSpecified = true;

            //three sets of resolvers in the group:
            itinerary.ResolverGroups = new asmxItinerary2Way.ArrayOfItineraryResolversResolvers[3];
            itinerary.ResolverGroups[0] = new asmxItinerary2Way.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[0].serviceId = string.Format("{0}{1}", mapService.name, mapService.position);
            itinerary.ResolverGroups[0].Value = @"<![CDATA[STATIC:\\TransformType=ESBSimpleSamples.BizTalk.Maps.Parameters_GetUnicodeCodeRequest,ESBSimpleSamples.BizTalk, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a9e52fd7be56a687;]]>";

            itinerary.ResolverGroups[1] = new asmxItinerary2Way.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[1].serviceId = string.Format("{0}{1}", routeService.name, routeService.position);
            itinerary.ResolverGroups[1].Value = @"<![CDATA[STATIC:\\TransportLocation=http://localhost/ESBSimpleSamples/EncodingService.asmx;Action=http://ESBSimpleSamples.BizTalk.Schemas/1.0/GetUnicodeCode;TargetNamespace=http://ESBSimpleSamples.BizTalk.Schemas/1.0/;]]>";

            itinerary.ResolverGroups[2] = new asmxItinerary2Way.ArrayOfItineraryResolversResolvers();
            itinerary.ResolverGroups[2].serviceId = string.Format("{0}{1}", sendService.name, sendService.position);


            asmxItinerary2Way.Process process = new asmxItinerary2Way.Process();
            process.Credentials = System.Net.CredentialCache.DefaultCredentials;

            process.ItineraryValue = itinerary;
            XmlNode node = new XmlDocument();
            node.InnerXml = string.Format(@"<ns0:Parameters xmlns:ns0=""http://ESBSimpleSamples.BizTalk.Schemas.Parameters""><ns0:Parameter Name=""Character"" Value=""{0}""/></ns0:Parameters>", character);
            process.SubmitRequestResponse(ref node);

            byte[] bytes = Convert.FromBase64String(node.InnerText);
            return bytes;
        }
    }
}
